﻿using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiMeiJiaLibModel.Enums;
using YiMeiJiaService.Assembly;
using YiMeiJiaService.IService;
using YiMeiJiaLibModel.Config;
namespace YiMeiJiaApp.Pages.Account
{

    public partial class Login
    {
        #region 服务组件
        [Inject]
        private IAcccount _account { get; set; }
        [Inject]
        protected NavigationManager Navigation { get; set; }
        [Inject]
        protected IJSRuntime JSRuntime { get; set; }
        #endregion

        #region 参数组件
        /// <summary>
        /// 电话号码
        /// </summary>
        private string mobileNumber = "";
        /// <summary>
        /// 输入的验证码
        /// </summary>
        private string inputCode = "";
        /// <summary>
        /// 验证码的按钮
        /// </summary>
        private static string codeBtnTxt = "发送验证码";

        /// <summary>
        /// 是否允许发送
        /// </summary>
        private static bool sended = false;
        private static bool vierifyLogin = false;  // 是否允许点登录


        public string _XTitle;
        public string _XConten;

        #endregion


        public async Task sets()
        {
            await new EvalJs().NotificationStandard(JSRuntime, new YiMeiJiaLibModel.Assembly.JsNotification()
            {
                title = "提示啊",
                text = "懒",
                media = $"{YiMeiJiaLibModel.Config.ApiConf.CND}/Images/Icon/home.png",
                data = "noAuthorize=1",
                onClick = "/Home/Index"
            });
        }

        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <returns></returns>
        private async Task SendCode()
        {
#if DEBUG
            mobileNumber = "18306666961";
#endif
            if (sended)
            {
                try
                {
                    var res = await JSRuntime.InvokeAsync<int>("eval", @" _CheckTime();");
                    if (res > 0)
                    {
                        await EvalJs.ExJs().Toast(JSRuntime, $"请等待{res}秒后再点击发送按钮", EmToast.forbidden);
                        return;
                    }
                }
                catch (Exception ex)
                {

                    throw;
                }
            }

            if (string.IsNullOrEmpty(mobileNumber))
            {
                await EvalJs.ExJs().Toptip(JSRuntime, "请输入手机号码", EmToptip.error);
                return;
            }
            if (!_Verify.IsPhoneNumber(mobileNumber))
            {
                await EvalJs.ExJs().Toptip(JSRuntime, "手机格式不正确", EmToptip.error);
                return;
            }
            sended = true;

            Random rd = new Random();
            int codeNumber = rd.Next(100000, 999999);  // 测试环境随机创建
            await EvalJs.ExJs().LoadMask(JSRuntime);
            var response = await _HttpClient.Action<string,string>("sendSms", new { mobile = mobileNumber, code = codeNumber });
            await EvalJs.ExJs().LoadMask(JSRuntime, true);
            if (response.status)
            {
                vierifyLogin = true;
                var eval_js = @"
                            send_time =120 ; 
                            time_c();
";
                await JSRuntime.InvokeVoidAsync("eval", eval_js);
#if DEBUG
                await EvalJs.ExJs().Toast(JSRuntime, $"测试环境： 发送成功，您的验证码是{response.msg}", EmToast.succee);
                inputCode = response.msg;
#endif
                await EvalJs.ExJs().Toast(JSRuntime, $"测试环境： 发送成功，您的验证码是{response.msg}", EmToast.succee);
                inputCode = response.msg;

            }
            else
                sended = false;
        }
        private void TimerCallback(object state)
        {

            // 按钮恢复正常使用  
        }
        /// <summary>
        /// 提交登录
        /// </summary>
        public async Task SubmitLoginAsync()
        {
            try
            {
                if (!vierifyLogin)
                {
                    await new EvalJs().Toast(JSRuntime, "请获取验证码", EmToast.cancel);
                    return;
                }

                // 执行登录
                await EvalJs.ExJs().LoadMask(JSRuntime);
                var response = await _account.Login(mobileNumber, inputCode);
                await EvalJs.ExJs().LoadMask(JSRuntime, true);
                if (response.status)
                    Navigation.NavigateTo("/");
                else
                    await EvalJs.ExJs().Toast(JSRuntime, response.msg, EmToast.cancel);
            }
            catch (Exception ex )
            {
                await EvalJs.ExJs().LoadMask(JSRuntime, true);
                throw;
            }

        }


        public async Task GetDataAndPop(string name)
        {
            var _data =await  _HttpClient.Get($"{ApiConf.ApiStatic()}/data/{name}.txt");
            _XTitle = name;
            _XConten = _data;
        }
    }
}
